package team240;

import java.util.ArrayList;

import team240.Robots.Status;
import battlecode.common.*;

public class Soldier extends GroundRobot{
	RobotController myRC;
	ArrayList<Robots> robotArray;
	
	public Soldier(RobotController inRC) {
		super(inRC);
		myRC = inRC;
	}

	public void runSoldier() {
		//main run method for the archon
		try {
			while (true) {
				//System.out.println(myStatus);
				switch(myStatus) {
				case SEARCH: search(); break;
				case COMBAT: combat(); break;
				case MINE: mine(); break;
				case TRAVEL: travel(); break;
				case SPAWN: spawn(); break;
				default: //error unrecognized status
				}
			}
		} catch(Exception e) {
			System.out.println("caught exception:");
			e.printStackTrace();
		}
	}
	
	public void combat() {
		try {
			robotArray = scanSurroundingRobots();
			
			Robot targetRobot = findTargetRobot(robotArray);
				
			
			//helper methods here
			//for example: lookForEnemy()
			//transferEnergon()
			myRC.yield();
        } catch (Exception e) {
           System.out.println("caught exception:");
           e.printStackTrace();
        }
	}
	
	/**
	 * Find closest robot
	 * @param robots -- robots in range
	 * @return the robot we want to attack
	 * @throws GameActionException 
	 */
	private Robot findTargetRobot(ArrayList<Robots> robots) {
		ArrayList<Robot> enemyRobots = new ArrayList<Robot>();
		for (Robot robot : robots) {
			try {
				RobotInfo info = myRC.senseRobotInfo( robot );
				if ( info.team.opponent() == myRC.getTeam() ) {
					enemyRobots.add( robot );
				}
				//Attach enemy robot that is closest to soldier
			} catch (GameActionException e) {
				System.out.println("Solider wasn't apply to sense Robot's info!");
			}
	}
		
		return null;
	}

	private ArrayList<Robots> scanSurroundingRobots() {
		ArrayList<Robots> robots = new ArrayList<Robots>();
		
		Robot[] groundRobots = myRC.senseNearbyGroundRobots();
		Robot[] airRobots = myRC.senseNearbyAirRobots();
		
		for (Robot x : groundRobots) {
			robots.add((Robots) x);
		}
		
		for (Robot x : airRobots) {
			robots.add((Robots) x);
		}
		
		return robots;
	}

	public void search() {
		myRC.yield();	
	}
	
	public void mine() {
		myRC.yield();	
	}
	
	public void travel() {
		myRC.yield();	
	}
	
	public void spawn() {
		myRC.yield();	
	}

	
}